<template>
  <el-form
    ref="ruleForm"
    :model="ruleForm"
    
    :rules="rules"
    label-width="150px"
    class="app-container"
  >
    <el-form-item :label="this.$t('account.oldPwd')" prop="oldPassword">
      <PwdBtn v-model="ruleForm.oldPassword" />
    </el-form-item>
    <el-form-item
   
      :label="this.$t('account.newPwd')"
      prop="newPassword"
    >
      <PwdBtn v-model="ruleForm.newPassword" />
    </el-form-item>
    <el-form-item :label="this.$t('account.confirmPwd')" prop="confirmPwd">
      <PwdBtn v-model="ruleForm.confirmPwd" />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">
        {{ $t('btns.confirm') }}
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import PwdBtn from '@/components/PwdBtn'

export default {
  name: 'MP',
  components: {
    PwdBtn
  },
  data() {
    const validatePass = (rule, value, callback) => {
      if (value) {
        if (this.ruleForm.confirmPwd !== '') {
          this.$refs.ruleForm.validateField('confirmPwd')
        }
        const pwdRegEn = /[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/
        if (!pwdRegEn.test(value)) {
          callback(new Error(this.$t('account.pwdFormat')))
          return
        }
      }
      callback()
    }
    const validatePass2 = (rule, value, callback) => {
      if (value && value !== this.ruleForm.newPassword) {
        callback(new Error(this.$t('account.pwdSame')))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {},
      rules: {
        oldPassword: [
          {
            required: true,
            message: this.$t('account.oldPwdReq'),
            trigger: 'blur'
          }
        ],
        newPassword: [
          {
            required: true,
            message: this.$t('account.newPwdReq'),
            trigger: 'blur'
          },
          {
            min: 6,
            message: this.$t('account.pwdMin'),
            trigger: 'blur'
          },
          {
            max: 12,
            message: this.$t('account.pwdMax'),
            trigger: ['change', 'blur']
          },
          { validator: validatePass, trigger: 'blur' }
        ],
        confirmPwd: [
          {
            required: true,
            message: this.$t('account.confirmPwdReq'),
            trigger: 'blur'
          },
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) {
          return
        }

        this.$store.dispatch('personalcenter/onUpdatePwd', this.ruleForm)
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped lang="scss">
.app-container {
 
}
</style>
